import store from '@/store';
import api from '@/api';
import { useMessage } from '@/hooks/common/useMessage';
import { message } from 'ant-design-vue';
export const useCreateTaskForm = (setForm, detail = {}) => {
    const handleSendByMy = () => {
        const { orgList, name, empId } = store.getters.userInfo.employeeDTO;
        setForm({
            initiator: name,
            initiatorDept: orgList[0].name,
            initiatorEmpId: empId
        });
    };
    // 通用选择回调
    const handleSelectChange = (val, field: string) => {
        switch (field) {
            case 'initiator':
                setForm({
                    initiator: val.name
                });
                if (val.empId) {
                    setForm({
                        initiatorEmpId: val.empId
                    });
                    getUserDept(val.empId);
                } else {
                    setForm({
                        initiatorEmpId: '',
                        initiatorDept: ''
                    });
                }
                break;
            case 'agentUserName':
                console.log(val);
                setForm({
                    agentUserName: val.empName,
                    agentUser: val.empId
                });
                break;
            case 'requestSystem':
                setForm({
                    requestSystem: val ? val.systemId.toString() : null,
                    pmpLabels: [],
                    catalogCode: val.catalogCode
                });
                break;
            case 'label':
                setForm({
                    pmpLabels: [...val]
                });
                (detail as any).id && bindLabel([...val]);
                break;
        }
    };
    const bindLabel = async (val: any[]) => {
        val = val.length ? val.map((item) => ({
            ...item,
            requireId: (detail as any).id,
            systemId: +(detail as any).requestSystem,
            type: 2
        })) : [];
        // 后台需要这么传值，代表空
        if (!val.length) {
            val = [
                {
                    requireId: (detail as any).id,
                    systemId: +(detail as any).requestSystem,
                    type: 2
                }
            ];
        }
        const { success, errorMsg } = await api.labelModel.bindLabel(val);
        if (!success) {
            message.error(errorMsg);
        }
    };
    // 查询部门
    const getUserDept = async (empId: number) => {
        const query = {
            empId
        };
        const { success, moule } = await api.userModel.getEmployeeByEmpId(query);
        const orgNameArr = moule.orgName ? moule.orgName.split('-') : [];
        if (success) {
            if (moule) {
                setForm({
                    initiatorDept: orgNameArr.length ? orgNameArr[orgNameArr.length - 1] : ''
                });
            }
        }
    };
    // 监听文件删除等操作
    const fileChange = (fileList: any[]) => {
        setForm({
            selfRequestFiles: [...fileList]
        });
    };
    const beforeSubmit = (callback) => {
        const { createConfirm } = useMessage();
        createConfirm({
            title: '业务需求流程发起后将不可修改，是否确定发起需求？',
            iconType: 'warning',
            onOk: () => {
                callback();
            }
        });
    };
    return {
        handleSendByMy,
        handleSelectChange,
        fileChange,
        beforeSubmit
    };
};